package com.waypolice.manager.service.impl;

import com.waypolice.common.pdfUtil.Extract_Text;
import com.waypolice.common.utils.FastDfsClient;
import com.waypolice.manager.service.PDFFileConversionService;
import org.apache.http.HttpRequest;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * ClassName: PDFFileConversionServiceImpl
 * Function:  PDF文件转换接口实现类
 * Date:      2019/8/8 10:33
 * author     yeruixaing
 * QQ:        3141684159
 * version    V1.0
 */
@Service
public class PDFFileConversionServiceImpl implements PDFFileConversionService {

    /**
     * @Author yeruixiang
     * @Description 下载dsf文件到本地并解析pdf文件
     * @Date 2019/8/8 10:36
     * @Param [fast_FileName_Path]
     * @return java.lang.String
     **/
    @Override
    public String PDFFileConversions(String fast_FileName_Path) {
        String pdfConversion = null;
        //判断fast服务器路径是否为空
        if(fast_FileName_Path == null && fast_FileName_Path == ""){
            return "文件路径不可为空！";
        }
            try {
                FastDfsClient fastDFSClient = new FastDfsClient("classpath:client.conf");
                StringBuffer sb = new StringBuffer();
                //获取文件绝对路径
                String str = System.getProperty("user.dir");
                String path = str.substring(0, 2);
                //截取下载到本地的文件名进行处理
                int fileNum = fast_FileName_Path.lastIndexOf("/");
                String fileName = fast_FileName_Path.substring(fileNum+1,fast_FileName_Path.length());
                sb.append(path);
                sb.append("/solrPDF/");
                //获取fastdfs文件路径
                String fastPathFileName = fast_FileName_Path.substring(27);
                File file = new File(sb.toString());
                if (!file.exists()) {
                    file.mkdirs();
                }
                sb.append(fileName);
                //将fast服务器文件下载到本地
//                int count = fastDFSClient.downloadFile(fastPathFileName, sb.toString());
                fastDFSClient.downloadFile(fastPathFileName, sb.toString());
                //调用PDF解析方法解析后并返回
                pdfConversion = Extract_Text.extractUtil(sb.toString());
                if(pdfConversion != null && !"".equals(pdfConversion)){
                    Extract_Text.deleteFile(sb.toString());
                }
                System.out.println("pdfConversion = " + pdfConversion);
            }catch(Exception e){
                e.printStackTrace();
                return "PDF文件解析失败！";
            }
        return pdfConversion;
    }
}
